package client;
/**
 * 
 */


import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * Listenes on UDP socket and output to console
 * @author Gernot
 *
 */
public class ClientListener extends Thread {
    private static final int BUFFERSIZE = 255;
    private DatagramSocket udp_s;
    private boolean running;
    
    public ClientListener(int localport) throws SocketException {
        udp_s = new DatagramSocket(localport);
        running = true;
        this.start();
    }

    public void stopListening(){
        this.udp_s.close();
    }
    
    @Override
    public void run() { 
        byte[] buf = new byte[BUFFERSIZE];
        DatagramPacket p = new DatagramPacket(buf, BUFFERSIZE);
        
        while (running) {
            try {
                udp_s.receive(p);
                String str = new String(p.getData());
                //Clear buffer
                for (int i=0;i<BUFFERSIZE; i++){
                    buf[i]=' ';
                }
                //buf = new byte[BUFFERSIZE];
                
                
                if (str.startsWith("!new-bid")){
                    //str.matches("\\!new\\-bid.*")){
                    System.out.println("You have been overbid on '"+str.substring(8).trim()+"'");
                    System.out.print(Client.username+"> ");
                } else if (str.startsWith("!auction-ended")){
                    //str.matches("\\!auction\\-ended \\S+ \\d+\\.?\\d* \\S+")){
                    String[] cmd = str.split(" ");
                    
                    String name = cmd[3];
                    for (int i = 4; i<cmd.length; i++){
                        name = name + " "+cmd[i];
                    }   
                    
                    //System.out.println("-->|"+cmd[1]+"|"+username+"|");
                    if (cmd[1].equals(Client.username)) {    
                        System.out.println("The auction '"+name.trim()+"' has ended. You won with "+cmd[2]+"!");
                    } else {
                        System.out.println("The auction '"+name.trim()+"' has ended. "+cmd[1]+" won with "+cmd[2]+".");
                    }
                    System.out.print(Client.username+"> ");
                /*} else if (str.startsWith("Successfully logged in as ")){
                    String str2 = str.substring("Successfully logged in as ".length()).trim();
                    username = str2.substring(0,str2.length()-1);
                    //System.out.println("Username set to: |"+username+"|");
                    System.out.println(str.trim());
                */} else {
                    System.out.println(str.trim());
                }
                 
            } catch (SocketException s){
                //Stop
                running = false;
            } catch (IOException e) {
               System.out.println("Error: "+e.getMessage());
            }
        }
        
    }
    
    
}
